
class PlateCustom:
    ROW_LIST = []
    COL_LIST = []

    def __init__(self, id, row, col) -> None:
        self.id = id
        self.row_len = row
        self.col_len = col
        self.data:list[list] = [[0]*self.col_len for i in range(self.row_len)]
        self.point:list[int] = [0, 0]  ###当前行列索引

    def move(self, row, col):
        '''指针移动到相应位置'''

        self.point = [row, col]

    def put_item(self, row:int|None, col:int, item):
        '''在指定位置赋值，但指针位置不变\n
        在 row 设定为 None 时，程序会将 col 作为索引'''

        if(row == None):
            row = col % self.row_len
            col = col // self.row_len

        self.data[row][col] = item

    def get_item(self, row, col):
        '''获取目标位置的值\n
        超出索引上限时返回空'''
        if(row >= self.row_len or col >= self.col_len):
            return None

        return self.data[row][col]

    def get_point(self):
        '''获取指针位置'''

        return self.point

    def put_row(self, item):
        '''在指针位置赋值，指针列索引值加一'''
        self.put_item(self.point[0],self.point[1],item)
        self.point[1] += 1

        if(self.point[1] >= self.col_len):
            self.point[1] = 0
            self.point[0] = self.point[0] + 1
        

    def put_col(self, item):
        '''在指针位置赋值，指针行索引值加一'''
        self.put_item(self.point[0],self.point[1],item)
        self.point[0] += 1

        if(self.point[0] >= self.row_len):
            self.point[0] = 0
            self.point[1] = self.point[1] + 1
        

    def get_data(self):
        '''获取板内的所有孔位的信息'''

        return self.data

    def print(self):
        print(",".join([self.id] + self.COL_LIST))
        for r in range(self.row_len):
            print(",".join([self.ROW_LIST[r]] + [str(i) for i in self.data[r]]))

    def search_item(self, item):
        for i in range(self.row_len):
            for j in range(self.col_len):
                if(self.get_item(i, j) == item):
                    return [i, j]
        return None
    
    def to_list(self):
        '''孔板转列表，输出格式为 [板号, 孔号, oligo名]'''
        res:list[list[str]] = []
        for j in range(self.col_len):
            for i in range(self.row_len):
                if(self.get_item(i, j) and self.get_item(i, j) != 0 and self.get_item(i, j) != "" and self.get_item(i, j) != '0'):
                    res.append([self.id, f"{self.ROW_LIST[i]}{self.COL_LIST[j]}", self.get_item(i, j)])
        return res



class Plate96(PlateCustom):
    ROW_LIST = [chr(i) for i in range(ord('A'),ord('I'))]
    COL_LIST = [str(i) for i in range(1, 13)]
    def __init__(self, id) -> None:
        #####8行12列
        super().__init__(id, 8, 12)

class Plate384(PlateCustom):
    ROW_LIST = [chr(i) for i in range(ord('A'),ord('Q'))]
    COL_LIST = [str(i) for i in range(1, 25)]
    def __init__(self, id) -> None:
        #####16行24列
        super().__init__(id, 16, 24)

